namespace CRMStats_Plugin   
{   
public class CRMStats_Plugin:IPlugin   
{   
public void Execute(IServiceProvider serviceProvider)   
{   
ITracingService tracingService=null;   
try  
{   
tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));   
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));   
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));   
if (context.Depth >= 2) return;   
IOrganizationService service = serviceFactory.CreateOrganizationService(null);   
Entity stat = new Entity("new_estadistica");   
stat.Attributes["new_name"] = context.MessageName;   
stat.Attributes["new_entidad"] = context.PrimaryEntityName;   
stat.Attributes["new_systemuserid"] = new EntityReference("systemuser", context.UserId);   
service.Create(stat);   
}   
catch (Exception ex)   
{   
tracingService.Trace("CRMStats_Plugin[Execute()]", "Execution Error: " + ex.Message + "-"+ex.StackTrace);   
throw ex;   
}   
}   
}   
}  
